// ui code mainly borrowed and adapted from the Oblique iphone project
#import "App.h"
#import "WaveSpeedView.h"
#import <LayerKit/LKBasicAnimation.h>


@implementation WaveSpeedView
-(id)initWithFrame:(CGRect)frame andParent:(id)parent{
	self = [super initWithFrame:frame];
	mParent = parent;
	
	
	CGSize navSize = [UINavigationBar defaultSize];
	
	_navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, navSize.height)];
	UINavigationItem* navtitle = [[UINavigationItem alloc] initWithTitle:@"Wave Speed"];
	[_navBar pushNavigationItem: navtitle];
	[_navBar showButtonsWithLeftTitle:@"Back" rightTitle:nil leftBack:YES];
	[_navBar setBarStyle:5]; // This sets the color and look of the navigation bar.
	[_navBar setDelegate:self];	

	_table = [[UIPreferencesTable alloc] initWithFrame:CGRectMake(frame.origin.x, frame.origin.y + navSize.height, frame.size.width, frame.size.height - navSize.height)];
	[_table setDataSource:self];
	[_table setDelegate:self];

	[self addSubview:_navBar];
	[self addSubview:_table];

	
	
	WaveSpeedCells = [[NSMutableArray alloc] initWithCapacity:4];
	int i;
	for (i=0;i<3;i++) {
		UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
		[cell setTitle: waveSpeedsLong[i]];
    	[WaveSpeedCells addObject:cell];
	}
	return self;
}

- (void)navigationBar:(UINavigationBar*)bar buttonClicked:(int)which {
	[mParent HideWaveSpeed: waveSpeed];
}

- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable*)aTable {
	return 1;
}

- (int)preferencesTable:(UIPreferencesTable *)aTable numberOfRowsInGroup:(int)group {
	return 3;
}

- (UIPreferencesTableCell*)preferencesTable:(UIPreferencesTable*)aTable cellForGroup:(int)group {
	return [[[UIPreferencesTableCell alloc] init] autorelease];
}

- (UIPreferencesTableCell*)preferencesTable:(UIPreferencesTable*)aTable cellForRow:(int)row inGroup:(int)group {
	return [WaveSpeedCells objectAtIndex:row];
}

- (BOOL)table:(UITable*)table canSelectRow:(int)row {
	return YES;
}

- (void)ShowWaveSpeed:(int)index{
	waveSpeed = index;
	UIPreferencesTableCell *cell;
	int i;
	for (i=0;i<3;i++) {
		cell = [WaveSpeedCells objectAtIndex:i];
		if (i==waveSpeed){
			[cell setChecked:YES];
			[cell setHighlighted:YES];
			[cell setSelected:YES withFade:YES];
		}
		else{
			[cell setChecked:NO];
			[cell setSelected:NO withFade:NO];
			[cell setHighlighted:NO];
		}
	}
	[_table reloadData];
}

- (void)tableRowSelected:(NSNotification*)notification {
	int selected = [_table selectedRow] - 1;
	[self ShowWaveSpeed:selected];
	[mParent HideWaveSpeed: waveSpeed];
}

@end


